package com.yhao.stock.common.utils;

import com.yhao.stock.common.dto.UndulateModel;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author : yhao
 * @Date : 2023/11/24 - 7:45
 * @Desc : TODO
 */
public class StockUtil2 {

    static Integer continuous = 5;
    static Integer deviation = 20;

    public static void main(String[] args) {
        String filePath = "D:\\a.txt";
        List<String> contents = FileUtil.readTxt(filePath);
        ArrayList<UndulateModel> undulateModels = new ArrayList<>();

        BigDecimal totalAvgPrice = totalAvgPrice(contents); // 全部平均价格
        BigDecimal totalMaxPrice = new BigDecimal(Collections.max(contents)); // 全部的最大值
        BigDecimal totalMinPrice = new BigDecimal(Collections.min(contents)); // 全部的最小值
        BigDecimal middleDiffPrice = totalMaxPrice.subtract(totalAvgPrice).setScale(2, RoundingMode.HALF_UP);
        UndulateModel undulateModel = null;
        for (int i = continuous; i < contents.size() - continuous; i++) {
            BigDecimal currPrice = new BigDecimal(contents.get(i));
            List<String> beforeFiveList = contents.subList(i - continuous, i);
            List<String> afterFiveList = contents.subList(i + 1, i + continuous + 1);
            if (currPrice.compareTo(totalAvgPrice(beforeFiveList)) > 0 &&
                    currPrice.compareTo(totalAvgPrice(afterFiveList)) > 0 &&
                    currPrice.compareTo(new BigDecimal(Collections.max(beforeFiveList))) > 0 &&
                    currPrice.compareTo(new BigDecimal(Collections.max(afterFiveList))) > 0) {
                if (currPrice.compareTo(totalAvgPrice) > 0) {
                    if (totalMaxPrice.subtract(currPrice).divide(middleDiffPrice, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(10)).compareTo(new BigDecimal(deviation)) < 0) {
                        undulateModel = new UndulateModel(i, currPrice, 1, false);
                        undulateModels.add(undulateModel);
                    }
                }
            }
            // }
            // 获取波谷
            // if (flag == 2 || flag == 1) {
            if (currPrice.compareTo(new BigDecimal(Collections.min(beforeFiveList))) < 0 &&
                    currPrice.compareTo(new BigDecimal(Collections.min(afterFiveList))) < 0 &&
                    currPrice.compareTo(totalAvgPrice(beforeFiveList)) < 0 &&
                    currPrice.compareTo(totalAvgPrice(afterFiveList)) < 0) {
                if (currPrice.compareTo(totalAvgPrice) < 0) {
                    if (currPrice.subtract(totalMinPrice).divide(middleDiffPrice, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(10)).compareTo(new BigDecimal(deviation)) < 0) {
                        undulateModel = new UndulateModel(i, currPrice, 0, false);
                        undulateModels.add(undulateModel);
                    }
                }
            }
        }

        ArrayList<UndulateModel> finalList = new ArrayList();
        UndulateModel middleUndulateMode = null;
        for (int i = 0; i < undulateModels.size(); i++) {
            UndulateModel currUndulateModel = undulateModels.get(i);
            if (i == 0) {
                middleUndulateMode = currUndulateModel;
            } else {
                if (middleUndulateMode.getFlag() == currUndulateModel.getFlag()) {// 同类
                    if (middleUndulateMode.getFlag() == 1) {// 波峰
                        if (middleUndulateMode.getPrice().compareTo(currUndulateModel.getPrice()) < 0) {
                            middleUndulateMode = currUndulateModel;
                        }
                    } else {// 波谷
                        if (middleUndulateMode.getPrice().compareTo(currUndulateModel.getPrice()) > 0) {
                            middleUndulateMode = currUndulateModel;
                        }
                    }
                } else {
                    finalList.add(middleUndulateMode);
                    middleUndulateMode = currUndulateModel;
                }
            }
        }

        for (int i = 0; i < finalList.size(); i++) {
            final UndulateModel undulateModel1 = finalList.get(i);
            if(undulateModel1.getFlag() == 0){
                System.out.println("波谷：索引=["+undulateModel1.getIndex()+"]  股价=["+undulateModel1.getPrice()+"]" );
            }else{
                System.out.println("波峰：索引=["+undulateModel1.getIndex()+"]  股价=["+undulateModel1.getPrice()+"]" );
            }
        }
    }

    public static BigDecimal totalAvgPrice(List<String> contents) {
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal count = new BigDecimal(contents.size());
        for (String content : contents) {
            totalPrice = totalPrice.add(new BigDecimal(content));
        }
        return totalPrice.divide(count, 2, RoundingMode.HALF_UP);
    }

}
